<template>
  <div id="changedialog">
    <commonformdialog
      :isshowDialogs="isshowDialog"
      :dialogTitle="dialogTitle"
      @closetextDialog="closetextDialog"
      @confirmbtn="confirmbtn"
      :dialogWidth="'750px'"
    >
      <div class="innerLoding">
        <el-row>
          <el-form
            :model="formData"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            size="small"
          >
            <el-row>
              <el-col :span="20">
                <el-form-item
                  :label="i == 0 ? '联系人：' : ''"
                  v-for="(p, i) in formData.more_pointArr"
                  :prop="'more_pointArr.' + i + '.contactsId'"
                  :key="i"
                >
                  <el-select
                    v-model="p.contactsId"
                    filterable
                    clearable
                    remote
                    :loading="loading"
                    :remote-method="
                      (query) => {
                        remoteMethod(query, p);
                      }
                    "
                    placeholder="输入完整的姓名或者联系方式将自动检索"
                    style="width: 80%"
                  >
                    <el-option
                      v-for="item in p.contactsOptions"
                      :key="item.id"
                      :label="
                        item.name +
                        ' ' +
                        item.tel +
                        ' ' +
                        item.wechat +
                        ' ' +
                        item.email
                      "
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                  <span
                   v-if="i == 0"
                    class="el-icon-circle-plus"
                    style="font-size: 20px; color: #1bbc9b; margin-left: 10px"
                    @click="addmoreTel"
                  ></span>
                  <span
                    v-if="i > 0"
                    class="el-icon-remove"
                    style="font-size: 20px; color: #ff4949; margin-left: 10px"
                    @click="removeTel(i)"
                  ></span>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-row>
      </div>
    </commonformdialog>
  </div>
</template>

<script type="text/javascript">
//引入弹窗
import commonformdialog from "@/components/commondialog/commondialog";

import { contactsSearch, relation } from "@/api/contacts";
export default {
  data() {
    return {
      showPicker: false,
      loading: false,
      multipleopen: true,
      cityoptionsc: [],
      cityoptionMd: [],
      num: 0,
      formData: {
        more_pointArr: [],
      },
      rules: {},
      isshowDialogMAP: false,
      dialogTitleMAP: "选择地址",
      mapData: {},
      selecte: true,
    };
  },
  props: {
    //是否显示弹窗
    isshowDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
    //表单的名字
    dialogTitle: {
      type: String,
      default: "",
      required: true,
    },
    //是编辑还是新增
    formType: {
      type: Number,
      default: 0,
    },

    dialogdata: {
      type: Object,
    },
  },
  components: {
    commonformdialog,
  },
  methods: {
    closetextDialog() {
      this.$emit("closedialog");
    },
    confirmbtn() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) return;
        let { objectId, objectType } = { ...this.formData };
        let contactsId = [];
        this.formData.more_pointArr.forEach((p) => {
          contactsId.push(p.contactsId);
        });
        let params = { objectId, objectType };
        params.contactsId = contactsId.join(",");
        let data = await relation(params);

        if (data.code == "0000") {
          this.$message({
            type: "success",
            message: data.msg,
          });
          this.$emit("confirm");
        }
      });
    },
    addmoreTel(){
    let newArr = this.formData.more_pointArr.concat([]);
      newArr.push({contactsId:'',contactsOptions:[],oldOptions:[]})
      let formData = { ...this.formData };
      formData.more_pointArr = newArr;
      this.formData = formData;
    },
    removeTel(i) {
      let newArr = this.formData.more_pointArr.concat([]);
      newArr.splice(i, 1);
      let formData = { ...this.formData };
      formData.more_pointArr = newArr;
      this.formData = formData;
    },
    async remoteMethod(query, p) {
      if (query !== "") {
         this.loading = true;
        let arr = p.oldOptions;
        let data = await contactsSearch({ keyword: query });
         this.loading = false;
        if (data.code == "0000") {
          arr = arr.concat(data.data);
          p.contactsOptions = arr.concat([]);
        }
      }
    },
  },
  watch: {
    isshowDialog: function (val) {
      if (!val) {
        this.$refs["ruleForm"].resetFields();
      }
    },
    dialogdata: function (val) {
      this.formData = val;
      let arr = val.contacts;
      let more_pointArr = [];
      if (arr.length > 0) {
        arr.forEach((p) => {
          let params = {};
          let option = [];
          option.push(p);
          params.contactsId = p.id;
          params.contactsOptions = option;
          params.oldOptions = option;
          more_pointArr.push(params);
        });
      }else{
        more_pointArr=[{contactsId:'',contactsOptions:[],oldOptions:[]}]
      }
      this.formData.more_pointArr = more_pointArr;
    },
  },
  created() {},
};
</script>

<style lang="scss" scoped>
::v-deep {
  #InputSearch {
    display: none !important;
  }
  .grid-emojis {
    grid-template-columns: repeat(10, 10%) !important;
  }
  .small {
    position: absolute;
    z-index: 9;
    left: -45px;
    top: 30px;
  }
  .cityoptionscItem {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
